<?php
defined('SYS_RUN') or exit();

class File {

    const ACCESS_RIGHTS = 0777;

    private $openedFile;

    private $nameOpenedFile;

    function __destruct() {
        $this->close();
    }

    public function getResolution($file) {
        return strtolower(substr(strrchr($file, '.'), 1));
    }

    public function getFilesList($dir) {
        clearstatcache();
        $files = array();
        if(is_dir($dir)) {
            $files = scandir($dir);
            array_shift($files);
            array_shift($files);
        }
        foreach($files as $key => $value){
            if(is_dir($dir.$value)){
                unset($files[$key]);
            }
        }
        return $files;
    }

    public function open($file, $maxSize =  null) {
        $this->nameOpenedFile = $file;
        @fclose(fopen($file, "a+b"));
        $this->openedFile = @fopen($file, "r+b") or die(Error::trigger('I can not open the file: '.$file));
        @flock($this->openedFile, LOCK_EX);
        $this->chmod($this->nameOpenedFile);
        if($maxSize) {
            if(filesize($file) >= $maxSize*1024*1024) {
                @ftruncate($this->openedFile, 0);
            }
        }
        return $this;
    }

    public function read() {
        $content = "";
        if($this->openedFile) {
            $content = @fread($this->openedFile, filesize($this->nameOpenedFile));
        } else {
            Error::trigger("Read. Обьект файла не существует или файл не открыт :".$this->nameOpenedFile);
        }
        return $content;
    }

    public function write($content, $isRewrite = true) {
        if($this->openedFile) {
            if(!$isRewrite) {
                fseek($this->openedFile, 0, SEEK_END);
            } else {
                ftruncate($this->openedFile,0);
            }
            @fwrite($this->openedFile, $content);

            fflush ($this->openedFile);
            $this->close();
        }
        return $this;
    }

    public function truncate() {
        if($this->openedFile) {
            @ftruncate($this->openedFile, 0);
        }
        return $this;
    }

    public function mkdir($dir, $isRecurse = false){
        $separator = '/';
        if(Sys::isWindows())
        {
            $separator = '\\\\';
            $dir = str_replace(array('/', '\\'), $separator, $dir);
        }
        if ($isRecurse === true){
            $parts = explode($separator, trim($dir));

            if(Sys::isWindows())
                $parentDirectory = '';
            else
                $parentDirectory = $separator;

            foreach ($parts as $part) {
                if(!$part) continue;
                if (!is_dir($parentDirectory . $part)) {
                    if(!is_writable($part))
                        @mkdir($parentDirectory . $part, self::ACCESS_RIGHTS, true);
                }
                $parentDirectory .= $part . $separator;
            }
        } else {
            if(!file_exists($dir)) mkdir($dir, self::ACCESS_RIGHTS, true);
        }
        return $this;
    }

    public function chmod($path, $access  = null){
        if(!$access) $access = self::ACCESS_RIGHTS;
        @chmod($path, $access );
    }

    public function close() {
        if($this->openedFile) {
            @flock ($this->openedFile,LOCK_UN);
            @fclose($this->openedFile);
        }
        $this->nameOpenedFile = '';
        return $this;
    }
}